package com.shaobufan.museum.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shaobufan.museum.mapper.StaffMapper;
import com.shaobufan.museum.pojo.entity.Staff;
import com.shaobufan.museum.pojo.vo.StaffVo;
import com.shaobufan.museum.service.DepartmentService;
import com.shaobufan.museum.service.StaffService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

import static com.shaobufan.museum.contant.StaffContent.STAFF_LOGIN_STATE;

/**
 * @author 17126
 * @description 针对表【staff(员工表)】的数据库操作Service实现
 * @createDate 2024-03-11 13:39:20
 */
@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff>
        implements StaffService {

    @Resource
    private StaffMapper staffMapper;
    @Resource
    private DepartmentService departmentService;

    /**
     * @param staffAccount  员工账户
     * @param staffPassword 员工密码
     * @return 员工
     */
    @Override

    public StaffVo staffLogin(String staffAccount, String staffPassword, HttpServletRequest request) {
        // 匹配账户密码
        QueryWrapper<Staff> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("staff_account", staffAccount);
        queryWrapper.eq("staff_password", staffPassword);
        // 取出用户实体
        Staff staff = staffMapper.selectOne(queryWrapper);
        // 使用Vo过滤数据
        StaffVo staffVo = getStaffVo(staff);
        // 设置session
        request.getSession().setAttribute(STAFF_LOGIN_STATE, staffVo);
        return staffVo;
    }

    /**
     * @param staffAccount  员工账户
     * @param staffPassword 员工密码
     * @return 员工
     */
    @Override
    public Integer staffRegister(String staffAccount, String staffPassword) {
        QueryWrapper<Staff> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("staff_account", staffAccount);
        Staff checkStaff = staffMapper.selectOne(queryWrapper);
        if (checkStaff != null) {
            return null;
        }
        Staff staff = new Staff();
        staff.setStaffAccount(staffAccount);
        staff.setStaffPassword(staffPassword);
        staffMapper.insert(staff);
        return staff.getStaffId();
    }

    /**
     * 修改员工
     *
     * @param staffId        员工 id
     * @param staffAccount   员工账户
     * @param staffPassword  员工密码
     * @param staffPrivilege 员工级别
     * @return 员工账户
     */
    @Override
    public String staffChange(Integer staffId, String staffAccount, String staffPassword, Integer staffPrivilege) {
        Staff staff = new Staff();
        staff.setStaffAccount(staffAccount);
        staff.setStaffPassword(staffPassword);
        staff.setStaffPrivilege(staffPrivilege);

        QueryWrapper<Staff> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("staff_id", staffId);

        int updated = staffMapper.update(staff, queryWrapper);
        if (updated != 1) {
            return null;
        }
        return staffMapper.selectOne(queryWrapper).getStaffAccount();
    }

    /**
     * 查询员工
     *
     * @param staffAccount 员工账号
     * @return 员工 list
     */
    @Override
    public List<StaffVo> staffSearch(String staffAccount) {
        if (ObjectUtils.isNotEmpty(staffAccount)) {
            QueryWrapper<Staff> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("staff_account", staffAccount);
            return staffMapper.selectList(queryWrapper).stream().map(this::getStaffVo).collect(Collectors.toList());
        }
        return staffMapper.selectList(null).stream().map(this::getStaffVo).collect(Collectors.toList());
    }

    /**
     * @param staff 员工实体
     * @return 员工实体Vo
     */
    @Override
    public StaffVo getStaffVo(Staff staff) {
        if (staff == null) {
            return null;
        }
        StaffVo staffVo = new StaffVo();
        staffVo.setStaffId(staff.getStaffId());
        staffVo.setStaffAccount(staff.getStaffAccount());
        staffVo.setStaffPrivilege(staff.getStaffPrivilege());
        staffVo.setDepartName(departmentService.searchDepartmentById(staff.getStaffPrivilege()).getDepartDetail());
        return staffVo;
    }
}